home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wxwindem.zip / HELLO.CC < prev    next >
C/C++ Source or Header  |  1993-04-19  |  14KB  |  535 lines

  1. /*
  2.  * File:     hello.cc
  3.  * Purpose:  Demo for wxWindows class library
  4.  *
  5.  *                       wxWindows 1.40
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                        Date: 18-4-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <windows.h> // Included only for using MS C/C++ precompiled headers
  30. #include "wx.h"
  31. #include "hello.h"
  32.  
  33. // Declare two frames
  34. MyFrame   *frame = NULL;
  35. wxFrame   *subframe = NULL;
  36. wxMenuBar *menu_bar = NULL;
  37. MyTimer   the_timer;
  38. Bool      timer_on = FALSE;
  39. wxBitmap  *test_bitmap = NULL;
  40. wxIcon    *test_icon = NULL;
  41.  
  42. // This statement initialises the whole application
  43. MyApp     myApp;
  44.  
  45. // For drawing lines in a canvas
  46. float     xpos = -1;
  47. float     ypos = -1;
  48.  
  49. // Must initialise these in OnInit, not statically
  50. wxPen     *red_pen;
  51. wxFont    *small_font;
  52.  
  53. float     zoom_factor = 1.0;
  54.  
  55. // The `main program' equivalent, creating the windows and returning the
  56. // main frame
  57. wxFrame *MyApp::OnInit(void)
  58. {
  59.   // Create a red pen
  60.   red_pen = new wxPen("RED", 3, wxSOLID);
  61.  
  62.   // Create a small font
  63.   small_font = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
  64.  
  65.   // Create the main frame window
  66.   frame = new MyFrame(NULL, "Hello wxWindows", 0, 0, 400, 550);
  67.  
  68.   // Give it a status line
  69.   frame->CreateStatusLine();
  70.  
  71.   // Give it an icon
  72.   test_icon = new wxIcon("aiai_icn");
  73.   frame->SetIcon(test_icon);
  74.  
  75.   // Make a menubar
  76.   wxMenu *file_menu = new wxMenu;
  77.  
  78.   file_menu->Append(HELLO_LOAD_FILE, "&Load file");
  79.  
  80.   wxMenu *pullright_menu = new wxMenu;
  81.   pullright_menu->Append(HELLO_TWIPS, "&Twips");
  82.   pullright_menu->Append(HELLO_LOMETRIC, "&10th mm");
  83.   pullright_menu->Append(HELLO_METRIC, "&Metric");
  84.   pullright_menu->Append(HELLO_NORMAL, "&Normal size");
  85.   pullright_menu->Append(HELLO_ZOOM, "&Zoom...");
  86.  
  87.   file_menu->Append(HELLO_SCALE, "&Scale picture", pullright_menu);
  88.  
  89.   file_menu->AppendSeparator();
  90.   file_menu->Append(HELLO_PRINT, "&Print");
  91. #ifdef wx_msw
  92.   file_menu->Append(HELLO_PRINT_EPS, "Print to &EPS file");
  93.   file_menu->Append(HELLO_COPY_MF, "&Copy metafile to clipboard");
  94. #endif
  95.   file_menu->AppendSeparator();
  96.   file_menu->Append(HELLO_QUIT, "&Quit");
  97.  
  98.   wxMenu *timer_menu = new wxMenu;
  99.   timer_menu->Append(HELLO_TIMER_ON, "Timer &on");
  100.   timer_menu->Append(HELLO_TIMER_OFF, "Timer o&ff");
  101.  
  102.   wxMenu *cursor_menu = new wxMenu;
  103.   cursor_menu->Append(wxCURSOR_ARROW, "Arrow");
  104.   cursor_menu->Append(wxCURSOR_WAIT, "Wait");
  105.   cursor_menu->Append(wxCURSOR_IBEAM, "Ibeam");
  106.   cursor_menu->Append(wxCURSOR_CROSS, "Cross");
  107.   cursor_menu->Append(wxCURSOR_SIZENWSE, "Size NWSE");
  108.   cursor_menu->Append(wxCURSOR_SIZENESW, "Size NESW");
  109.   cursor_menu->Append(wxCURSOR_SIZEWE, "Size WE");
  110.   cursor_menu->Append(wxCURSOR_SIZENS, "Size NS");
  111.   cursor_menu->Append(wxCURSOR_PENCIL, "Pencil");
  112.   cursor_menu->Append(wxCURSOR_BULLSEYE, "Bullseye");
  113.   cursor_menu->Append(wxCURSOR_MAGNIFIER, "Magnifier");
  114.   cursor_menu->Append(wxCURSOR_HAND, "Hand");
  115.   cursor_menu->Append(wxCURSOR_NO_ENTRY, "No Entry");
  116.   cursor_menu->Append(wxCURSOR_CHAR, "Char");
  117.   cursor_menu->Append(wxCURSOR_LEFT_BUTTON, "Left Button");
  118.   cursor_menu->Append(wxCURSOR_RIGHT_BUTTON, "Right Button");
  119.   cursor_menu->Append(wxCURSOR_MIDDLE_BUTTON, "Middle Button");
  120.   cursor_menu->Append(wxCURSOR_QUESTION_ARROW, "Question arrow");
  121.   cursor_menu->Append(wxCURSOR_SIZING, "Sizing");
  122.   cursor_menu->Append(wxCURSOR_SPRAYCAN, "Spraycan");
  123.   cursor_menu->Append(wxCURSOR_WATCH, "Watch");
  124.   cursor_menu->Append(wxCURSOR_POINT_LEFT, "Point left");
  125.   cursor_menu->Append(wxCURSOR_POINT_RIGHT, "Point right");
  126.   cursor_menu->Append(wxCURSOR_PAINT_BRUSH, "Paint brush");
  127.  
  128.   wxMenu *help_menu = new wxMenu;
  129.   help_menu->Append(HELLO_ABOUT, "&About");
  130.  
  131.   menu_bar = new wxMenuBar;
  132.  
  133.   menu_bar->Append(file_menu, "&File");
  134.   menu_bar->Append(timer_menu, "&Timer");
  135.   menu_bar->Append(cursor_menu, "&Cursor");
  136.   menu_bar->Append(help_menu, "&Help");
  137.  
  138.   // Associate the menu bar with the frame
  139.   frame->SetMenuBar(menu_bar);
  140.  
  141.   menu_bar->Enable(HELLO_TIMER_ON, TRUE);
  142.   menu_bar->Enable(HELLO_TIMER_OFF, FALSE);
  143.  
  144.   // Make a panel
  145.   frame->panel = new wxPanel(frame, 0, 0, 400, 250, wxBORDER);
  146.   frame->panel->SetLabelPosition(wxVERTICAL);
  147.  
  148.   // Create some panel items
  149.  
  150.   (void)new wxButton(frame->panel, (wxFunction)&button_proc, "A button");
  151.  
  152.   (void)new wxText(frame->panel, NULL, "A text item", "Initial value",
  153.                    -1, -1, 200);
  154.  
  155.   (void)new wxCheckBox(frame->panel, NULL, "A check box");
  156.  
  157.   frame->panel->NewLine();
  158.  
  159.   char *choice_strings[] = { "Julian", "Hattie", "Ken", "Dick" };
  160.  
  161.   wxChoice *choice = new wxChoice(frame->panel, NULL, "A choice item",
  162.                      -1, -1, -1, -1, 4, choice_strings);
  163.   choice->SetSelection(0);
  164.  
  165.   (void)new wxMessage(frame->panel, "Hello! A simple message");
  166.  
  167.   wxListBox *list = new wxListBox(frame->panel, (wxFunction)&list_proc, "A list",
  168.                                   wxSINGLE, -1, -1, 100, 100);
  169.   list->Append("Apple");
  170.   list->Append("Pear");
  171.   list->Append("Orange");
  172.   list->Append("Banana");
  173.   list->Append("Fruit");
  174.   
  175.   frame->panel->NewLine();
  176.  
  177.   (void)new wxSlider(frame->panel, NULL, "A slider",
  178.                      40, 22, 101, 150);
  179.  
  180.   (void)new wxMultiText(frame->panel, (wxFunction)NULL, "Multiline text", "Some text");
  181.  
  182.   // Make a text window
  183.   frame->text_window = new wxTextWindow(frame, 0, 250, 400, 250);
  184.  
  185.   // Make another frame, containing a canvas
  186.   subframe = new wxFrame(NULL, "Canvas Frame", 300, 300, 400, 300);
  187.  
  188.   int width, height;
  189.   subframe->GetClientSize(&width, &height);
  190.  
  191.   MyCanvas *canvas = new MyCanvas(subframe, 0, 0, width, height, wxRETAINED);
  192.   wxCursor *cursor = new wxCursor(wxCURSOR_PENCIL);
  193.   canvas->SetCursor(cursor);
  194.  
  195.   // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
  196.   canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
  197.   canvas->SetPen(red_pen);
  198.   frame->canvas = canvas;
  199.  
  200.   frame->Show(TRUE);
  201.   subframe->Show(TRUE);
  202.  
  203.   // Load a file into the text window
  204.   frame->text_window->LoadFile("welcome.txt");
  205.   frame->SetStatusText("Hello, wxWindows");
  206.  
  207.   // Return the main frame window
  208.   return frame;
  209. }
  210.  
  211. // Define my frame constructor
  212. MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
  213.   wxFrame(frame, title, x, y, w, h)
  214. {
  215.   panel = NULL;
  216.   text_window = NULL;
  217. }
  218.  
  219. // Intercept menu commands
  220. void MyFrame::OnMenuCommand(int id)
  221. {
  222.   wxDC *dc = canvas->GetDC();
  223.   switch (id)
  224.   {
  225.     case HELLO_LOAD_FILE:
  226.     {
  227.       char *s = wxFileSelector("Load text file", NULL, NULL, NULL, "*.txt");
  228.       if (s)
  229.         frame->text_window->LoadFile(s);
  230.       break;
  231.     }
  232.     case HELLO_TWIPS:
  233.     {
  234.       dc->SetMapMode(MM_TWIPS);
  235.       dc->Clear();
  236.       Draw(*dc);
  237.       break;
  238.     }
  239.     case HELLO_METRIC:
  240.     {
  241.       dc->SetMapMode(MM_METRIC);
  242.       dc->Clear();
  243.       Draw(*dc);
  244.       break;
  245.     }
  246.     case HELLO_LOMETRIC:
  247.     {
  248.       dc->SetMapMode(MM_LOMETRIC);
  249.       dc->Clear();
  250.       Draw(*dc);
  251.       break;
  252.     }
  253.     case HELLO_NORMAL:
  254.     {
  255.       dc->SetUserScale(1.0, 1.0);
  256.       dc->SetMapMode(MM_TEXT);
  257.       dc->Clear();
  258.       Draw(*dc);
  259.       break;
  260.     }
  261.     case HELLO_ZOOM:
  262.     {
  263.       char *zoom_factor_s = FloatToString(zoom_factor);
  264.       zoom_factor_s = wxGetTextFromUser("Enter new zoom factor", "Input", zoom_factor_s);
  265.       if (zoom_factor_s)
  266.       {
  267.         StringToFloat(zoom_factor_s, &zoom_factor);
  268.         dc->SetUserScale(zoom_factor, zoom_factor);
  269.         dc->Clear();
  270.         Draw(*dc);
  271.       }
  272.       break;
  273.     }
  274.     case HELLO_QUIT:
  275.     {
  276.       OnClose();
  277.       delete this;
  278.       break;
  279.     }
  280.     case HELLO_PRINT:
  281.     {
  282.       wxDC dc(NULL, NULL, NULL);  // Defaults to EPS under UNIX,
  283.                                   // normal Windows printing under Win 3.1
  284.       if (dc.Ok())
  285.       {
  286.         dc.StartDoc("Hello printout");
  287.         dc.StartPage();
  288.         Draw(dc);
  289.         dc.EndPage();
  290.         dc.EndDoc();
  291.       }
  292.       break;
  293.     }
  294. #ifdef wx_msw
  295.     case HELLO_PRINT_EPS:         // To use the special wxWindows EPS driver
  296.                                   // under Windows 3.1, specify "PostScript"
  297.     {
  298.       wxDC dc(NULL, "PostScript", NULL);
  299.       if (dc.Ok())
  300.       {
  301.         dc.StartDoc("Hello printout");
  302.         dc.StartPage();
  303.         Draw(dc, FALSE);
  304.         dc.EndPage();
  305.         dc.EndDoc();
  306.       }
  307.       break;
  308.     }
  309.     case HELLO_COPY_MF:
  310.     {
  311.       wxMetaFileDC dc;
  312.       if (dc.Ok())
  313.       {
  314.         Draw(dc, FALSE);
  315.         wxMetaFile *mf = dc.Close();
  316.         if (mf)
  317.         {
  318.           Bool success = mf->SetClipboard();
  319.           delete mf;
  320.         }
  321.       }
  322.       break;
  323.     }
  324. #endif
  325.     case HELLO_ABOUT:
  326.     {
  327.       (void)wxMessageBox("wxWindows GUI library demo Vsn 1.40\nAuthor: Julian Smart J.Smart@ed.ac.uk\nAIAI (c) 1993", "About wxHello");
  328.       break;
  329.     }
  330.     case HELLO_TIMER_ON:
  331.     {
  332.       the_timer.Start(5000);
  333.       timer_on = TRUE;
  334.       menu_bar->Enable(HELLO_TIMER_ON, FALSE);
  335.       menu_bar->Enable(HELLO_TIMER_OFF, TRUE);
  336.       break;
  337.     }
  338.     case HELLO_TIMER_OFF:
  339.     {
  340.       the_timer.Stop();
  341.       menu_bar->Enable(HELLO_TIMER_ON, TRUE);
  342.       menu_bar->Enable(HELLO_TIMER_OFF, FALSE);
  343.       break;
  344.     }
  345.     default:
  346.     {
  347.       wxCursor *cursor = new wxCursor(id);
  348.       frame->canvas->SetCursor(cursor);
  349.       break;
  350.     }
  351.   }
  352. }
  353.  
  354. // Intercept menu item selection - only has an effect in Windows
  355. void MyFrame::OnMenuSelect(int id)
  356. {
  357.   char *msg = NULL;
  358.   switch (id)
  359.   {
  360.     case HELLO_LOAD_FILE:
  361.       msg = "Load a text file";
  362.       break;
  363.     case HELLO_SCALE:
  364.       msg = "Scale picture";
  365.       break;
  366.     case HELLO_TWIPS:
  367.       msg = "Twips scale";
  368.       break;
  369.     case HELLO_METRIC:
  370.       msg = "Metric scale (mm)";
  371.       break;
  372.     case HELLO_LOMETRIC:
  373.       msg = "Metric scale (0.1 mm)";
  374.       break;
  375.     case HELLO_NORMAL:
  376.       msg = "Pixel scale";
  377.       break;
  378.     case HELLO_ZOOM:
  379.       msg = "Zoom by specified amount";
  380.       break;
  381.     case HELLO_QUIT:
  382.       msg = "Quit program";
  383.       break;
  384.     case HELLO_PRINT:
  385.       msg = "Print picture";
  386.       break;
  387. #ifdef wx_msw
  388.     case HELLO_PRINT_EPS:
  389.       msg = "Print to EPS file";
  390.       break;
  391.     case HELLO_COPY_MF:
  392.       msg = "Copy metafile to clipboard";
  393.       break;
  394. #endif
  395.     case -1:
  396.       msg = "";
  397.       break;
  398.   }
  399.   if (msg)
  400.     frame->SetStatusText(msg);
  401. }
  402.  
  403. // Size the subwindows when the frame is resized
  404. void MyFrame::OnSize(int w, int h)
  405. {
  406.   if (panel && text_window)
  407.   {
  408.     int width, height;
  409.     GetClientSize(&width, &height);
  410.     panel->SetSize(0, 0, width, (int)(height/2));
  411.     text_window->SetSize(0, (int)(height/2), width, (int)(height/2));
  412.   }
  413. }
  414.  
  415. void MyFrame::Draw(wxDC& dc, Bool draw_bitmaps)
  416. {
  417.   dc.SetFont(small_font);
  418.   dc.SetPen(wxGREEN_PEN);
  419.   dc.DrawLine(0, 0, 200, 200);
  420.   dc.DrawLine(200, 0, 0, 200);
  421.  
  422.   dc.SetBrush(wxCYAN_BRUSH);
  423.   dc.SetPen(wxRED_PEN);
  424.   dc.DrawRectangle(100, 100, 100, 50);
  425.   dc.DrawRoundedRectangle(150, 150, 100, 50);
  426.  
  427.   dc.SetClippingRegion(150, 150, 100, 50);
  428.   dc.DrawText("This text should be clipped within the rectangle", 150, 170);
  429.   dc.DestroyClippingRegion();
  430.  
  431.   dc.DrawEllipse(250, 250, 100, 50);
  432.  
  433.   dc.DrawSpline(50, 200, 50, 100, 200, 10);
  434.  
  435.   dc.DrawLine(50, 230, 200, 230);
  436.  
  437.   dc.DrawText("This is a test string", 50, 230);
  438.  
  439.   if (draw_bitmaps)
  440.   {
  441.     // The easy way to draw bitmaps, using icons
  442.     dc.DrawIcon(test_icon, 250, 50);
  443.  
  444. /*  An alternative way to draw bitmaps.
  445.     wxDC *temp_dc = dc.CreateCompatibleDC();
  446.     temp_dc->SelectObject(test_bitmap);
  447.     dc.Blit(250, 50, BITMAP_WIDTH, BITMAP_HEIGHT, temp_dc, 0, 0);
  448.     delete temp_dc;
  449. */
  450.  
  451.   }
  452. }
  453.  
  454. // Define a constructor for my canvas
  455. MyCanvas::MyCanvas(wxFrame *frame, int x, int y, int w, int h, int style):
  456.  wxCanvas(frame, x, y, w, h, style)
  457. {
  458. }
  459.  
  460. // Define the repainting behaviour
  461. void MyCanvas::OnPaint(void)
  462. {
  463.   frame->Draw(*(GetDC()));
  464. }
  465.  
  466. // This implements a tiny doodling program! Drag the mouse using
  467. // the left button.
  468. void MyCanvas::OnEvent(wxEvent& event)
  469. {
  470.   SetPen(wxBLACK_PEN);
  471.   float x, y;
  472.   event.Position(&x, &y);
  473.   if (xpos > -1 && ypos > -1 && event.Dragging())
  474.   {
  475.     DrawLine(xpos, ypos, x, y);
  476.   }
  477.   xpos = x;
  478.   ypos = y;
  479. }
  480.  
  481. // Intercept character input
  482. void MyCanvas::OnChar(int ch)
  483. {
  484.   char buf[2];
  485.   buf[0] = (char)ch;
  486.   buf[1] = 0;
  487.   frame->SetStatusText(buf);
  488. }
  489.  
  490. // Define the behaviour for the frame closing
  491. // - must delete all frames except for the main one.
  492. Bool MyFrame::OnClose(void)
  493. {
  494.   if (subframe)
  495.     delete subframe;
  496.  
  497.   return TRUE;
  498. }
  499.  
  500. // Gets some user input, and sets the status line
  501. void button_proc(wxButton& but, wxEvent& event)
  502. {
  503.   int choice = wxMessageBox("Press OK to continue", "Try me", wxOK | wxCANCEL);
  504.  
  505.   if (choice == wxOK)
  506.   {
  507.     char *text = wxGetTextFromUser("Enter some text", "Text input", "");
  508.     if (text)
  509.     {
  510.       (void)wxMessageBox(text, "Result", wxOK);
  511.  
  512.       frame->SetStatusText(text);
  513.     }
  514.   }
  515. }
  516.  
  517. // Put some text into the text window
  518. void list_proc(wxListBox& list, wxEvent& event)
  519. {
  520.   *(frame->text_window) << event.string;
  521. }
  522.  
  523. void GenericOk(wxButton& but, wxEvent& event)
  524. {
  525.   wxDialogBox *dialog = (wxDialogBox *)but.GetParent();
  526.  
  527.   dialog->Show(FALSE);
  528. }
  529.  
  530. // Timer notify proc
  531. void MyTimer::Notify(void)
  532. {
  533.   *(frame->text_window) << "Timer went!" << "\n";
  534. }
  535.